package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.base.BaseTimeDialogFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.ProjectSearchContent;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.Date;

public class SearchProjectCollectFragment extends Fragment implements OnClickListener {
  public static final String EXTRA_CONTENT =
      "com.isunland.managesystem.entity.SearchProjectCollectFragment.EXTRA_CONTENT";

  private static final int CUSTOMER = 0;
  private static final int PROPERTY = 1;
  private static final int STAGES = 2;
  private static final int START_DATA_BEGIN = 3;
  private static final int START_DATA_END = 4;
  private static final int PROJECT_NAME = 5;
  private static final int REQUIRE_DATA_BEGIN = 6;
  private static final int REQUIRE_DATA_END = 7;
  private static final int PM_MANAGER = 8;
  private static final int KIND = 9;
  private static final int TYPE = 10;
  private static final int SALE_NAME = 11;
  private static final int IFOVER = 12;

  private TextView mTvCustomerCompany;
  private ImageView mIbCustomerCompany;
  //	    private TextView mTvProjectProperty;
  //	    private ImageView mIbProjectProperty;
  private TextView mTvProjectStages;
  private ImageView mIbProjectStages;
  private TextView mTvStartDateBegin;
  private ImageView mIbStartDateBegin;
  private TextView mTvStartDateEnd;
  private ImageView mIbStartDateEnd;
  //	    private Button mBtMore;
  //	    private LinearLayout mLlMore;
  private EditText mEtProjectName;
  //	    private TextView mTvRequireDateBegin;
  //	    private ImageView mIbRequireDateBegin;
  //	    private TextView mTvRequireDateEnd;
  //	    private ImageView mIbRequireDateEnd;
  private TextView mTvPmManager;
  private ImageView mIbPmManager;
  private TextView mTvProjectKindCode;
  private ImageView mIbProjectKindCode;
  //	    private TextView mTvProjectTypeCode;
  //	    private ImageView mIbProjectTypeCode;
  //	    private TextView mTvSalesName;
  //	    private ImageView mIbSalesName;
  //	    private TextView mTvifOver;
  //	    private ImageView mIbifOver;

  private String customerId = "";
  private String customerName = "";

  //	    private String propertyCode="";//性质
  //	    private String propertyName="";

  private String projectState = "";//阶段
  private String projectStateName = "";//阶段名

  private String startDateBegin = "";//(开工日期 开始)
  private String startDateEnd = "";//(开工日期 结束)

  private String projectName = "";//项目名称

  //		private String requireDateBegin="";//(开工日期 开始)
  //		private String requireDateEnd="";//(开工日期 结束)

  //		private String ifOver="";
  //		private String ifOverName="";

  private String pmId = "";
  private String pmName = "";

  //		private String projectTypeCode="";
  //		private String projectTypeName="";

  private String projectKindName = "";
  private String projectKindCode = "";

  //		private String salesId="";
  //		private String salesName="";

  private ProjectSearchContent mSearchContent;

  @Override public void onCreate(Bundle savedInstanceState) {

    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.query);
    mSearchContent = (ProjectSearchContent) getArguments().getSerializable(EXTRA_CONTENT);

    getData(mSearchContent);

    super.onCreate(savedInstanceState);
  }

  private void getData(ProjectSearchContent content) {
    customerId = content.getCustomerId();
    customerName = content.getCustomerName();

    //	    propertyCode=content.getPropertyCode();//性质
    //	    propertyName=content.getPropertyName();

    projectState = content.getProjectState();//阶段
    projectStateName = content.getProjectStateName();//阶段名

    startDateBegin = content.getStartDateBegin();//(开工日期 开始)
    startDateEnd = content.getStartDateEnd();//(开工日期 结束)

    projectName = content.getProjectName();//项目名称

    //		requireDateBegin=content.getRequireDateBegin();//(开工日期 开始)
    //		requireDateEnd=content.getRequireDateEnd();//(开工日期 结束)
    //
    //		ifOver=content.getIfOver();
    //		ifOverName=content.getIfOverName();

    pmId = content.getPmId();
    pmName = content.getPmName();

    //		projectTypeCode=content.getProjectTypeCode();
    //		projectTypeName=content.getProjectTypeName();

    projectKindName = content.getProjectKindName();
    projectKindCode = content.getProjectKindCode();

    //		salesId=content.getSalesId();
    //		salesName=content.getSalesName();

  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      case R.id.menu_item_confirm:
        vollelPostConfirm();
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  //回传 所选值
  private void vollelPostConfirm() {
    projectName = mEtProjectName.getText().toString();
    Intent backIntent = new Intent();
    //		ProjectSearchContent mSearchContent=new ProjectSearchContent(customerId, customerName,
    //				propertyCode, propertyName, projectState, projectStateName, startDateBegin, startDateEnd,
    //				mEtProjectName.getText().toString(), requireDateBegin, requireDateEnd, ifOver, ifOverName, pmId, pmName,
    //			  projectTypeCode, projectTypeName, projectKindName, projectKindCode, salesId, salesName);

    ProjectSearchContent mSearchContent =
        new ProjectSearchContent(customerId, customerName, projectState, projectStateName,
            startDateBegin, startDateEnd, projectName, pmId, pmName, projectKindName,
            projectKindCode);
    backIntent.putExtra(ProjectCollectListFragment.EXTRA_CONTENT, mSearchContent);
    getActivity().setResult(Activity.RESULT_OK, backIntent);
    getActivity().finish();
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_search_project_collect, container, false);
    findView(v);
    return v;
  }

  private void findView(View v) {

    mTvCustomerCompany = (TextView) v.findViewById(R.id.tv_customer_company);
    mIbCustomerCompany = (ImageView) v.findViewById(R.id.ib_customer_company);
    //	        mTvProjectProperty = (TextView) v.findViewById(R.id.tv_project_property);
    //	        mIbProjectProperty = (ImageView) v.findViewById(R.id.ib_project_property);
    mTvProjectStages = (TextView) v.findViewById(R.id.tv_project_stages);
    mIbProjectStages = (ImageView) v.findViewById(R.id.ib_project_stages);
    mTvStartDateBegin = (TextView) v.findViewById(R.id.tv_startDateBegin);
    mIbStartDateBegin = (ImageView) v.findViewById(R.id.ib_startDateBegin);
    mTvStartDateEnd = (TextView) v.findViewById(R.id.tv_startDateEnd);
    mIbStartDateEnd = (ImageView) v.findViewById(R.id.ib_startDateEnd);

    //	        mBtMore = (Button) v.findViewById(R.id.bt_more);
    //	        mLlMore = (LinearLayout) v.findViewById(R.id.ll_more);

    mEtProjectName = (EditText) v.findViewById(R.id.et_projectName);
    //	        mTvRequireDateBegin = (TextView) v.findViewById(R.id.tv_requireDateBegin);
    //	        mIbRequireDateBegin = (ImageView) v.findViewById(R.id.ib_requireDateBegin);
    //	        mTvRequireDateEnd = (TextView) v.findViewById(R.id.tv_requireDateEnd);
    //	        mIbRequireDateEnd = (ImageView) v.findViewById(R.id.ib_requireDateEnd);
    mTvPmManager = (TextView) v.findViewById(R.id.tv_pmManager);
    mIbPmManager = (ImageView) v.findViewById(R.id.ib_pmManager);
    mTvProjectKindCode = (TextView) v.findViewById(R.id.tv_projectKindCode);
    mIbProjectKindCode = (ImageView) v.findViewById(R.id.ib_projectKindCode);
    //	        mTvProjectTypeCode = (TextView) v.findViewById(R.id.tv_projectTypeCode);
    //	        mIbProjectTypeCode = (ImageView) v.findViewById(R.id.ib_projectTypeCode);
    //	        mTvSalesName = (TextView) v.findViewById(R.id.tv_salesName);
    //	        mIbSalesName = (ImageView) v.findViewById(R.id.ib_salesName);
    //	        mTvifOver = (TextView) v.findViewById(R.id.tv_ifOver);
    //	        mIbifOver = (ImageView) v.findViewById(R.id.ib_ifOver);

    mIbCustomerCompany.setOnClickListener(this);
    //		mIbProjectProperty.setOnClickListener(this);
    mIbProjectStages.setOnClickListener(this);
    mIbStartDateBegin.setOnClickListener(this);
    mIbStartDateEnd.setOnClickListener(this);
    //		mIbRequireDateBegin.setOnClickListener(this);
    //		mIbRequireDateEnd.setOnClickListener(this);
    mIbPmManager.setOnClickListener(this);
    mIbProjectKindCode.setOnClickListener(this);
    //		mIbProjectTypeCode.setOnClickListener(this);
    //		mIbSalesName.setOnClickListener(this);
    //		mIbifOver.setOnClickListener(this);
    //
    //		mBtMore.setOnClickListener(this);
    initDate();
  }

  private void initDate() {

    mTvCustomerCompany.setText(customerName);
    //		mTvProjectProperty.setText(propertyName);
    mTvProjectStages.setText(projectStateName);
    mTvStartDateBegin.setText(startDateBegin);
    mTvStartDateEnd.setText(startDateEnd);
    mEtProjectName.setText(projectName);
    //		mTvRequireDateBegin.setText(requireDateBegin);
    //		mTvRequireDateEnd.setText(requireDateEnd);
    //		mTvifOver.setText(ifOverName);
    mTvPmManager.setText(pmName);
    mTvProjectKindCode.setText(projectKindName);
    //		mTvProjectTypeCode.setText(projectTypeName);
    //		mTvSalesName.setText(salesName);
  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      //		case R.id.bt_more:
      //			mBtMore.setVisibility(View.GONE);
      //			mLlMore.setVisibility(View.VISIBLE);//显示

      //			break;

      case R.id.ib_customer_company:
        //跳转到客户名录列表界面
        Intent i = new Intent(getActivity(), CustomerListChooseActivity.class);
        startActivityForResult(i, CUSTOMER);
        break;
      //项目性质,联网数据字典
      //		case R.id.ib_project_property:
      //			showDialog(PROPERTY);
      //			break;
      //项目阶段，本地写死
      case R.id.ib_project_stages:
        showDialog(STAGES);
        break;
      //开始时间
      case R.id.ib_startDateBegin:
        showDialog(START_DATA_BEGIN);

        break;
      //结束时间
      case R.id.ib_startDateEnd:
        showDialog(START_DATA_END);
        break;
      //		case R.id.ib_projectName:
      //			showDialog(PROJECT_NAME);
      //			break;
      //要求完成时间
      //		case R.id.ib_requireDateBegin:
      //			showDialog(REQUIRE_DATA_BEGIN);
      //			break;
      //			//要求结束时间
      //		case R.id.ib_requireDateEnd:
      //			showDialog(REQUIRE_DATA_END);
      //			break;
      //			//状态，本地
      //		case R.id.ib_ifOver:
      //			showDialog(IFOVER);
      //			break;
      //项目经理，选择人员
      case R.id.ib_pmManager:
        //			showDialog(PM_MANAGER);
        Intent pm = new Intent(getActivity(), AppointerListActivity.class);
        startActivityForResult(pm, PM_MANAGER);
        break;
      //项目类别，从数据字典获取
      case R.id.ib_projectKindCode:
        showDialog(KIND);
        break;
      //项目属性，从数据字典获取
      //		case R.id.ib_projectTypeCode:
      //			showDialog(TYPE);
      //			break;
      //			//销售代表
      //		case R.id.ib_salesName:
      //			Intent i1=new Intent(getActivity(),AppointerListActivity.class);
      //			startActivityForResult(i1, SALE_NAME);
      //			break;
      default:
        break;
    }
  }

  private void showDialog(int requestCode) {
    DialogFragment dialog = null;
    switch (requestCode) {
      //项目性质
      case PROPERTY:
        dialog = ProjectPropertyDialogFrament.newInstance(ProjectPropertyDialogFrament.property);
        break;
      //项目阶段
      case STAGES:
        dialog = new ProjectStagesDialogFragment();
        break;
      //开始时间
      case START_DATA_BEGIN:
        dialog = new BaseYMDTimeDialogFragment();
        break;
      //结束时间
      case START_DATA_END:
        dialog = new BaseYMDTimeDialogFragment();
        break;
      //			//项目名
      //		case PROJECT_NAME:
      //			dialog = new ChooseTypeDialogFragment();
      //			break;
      //开始时间
      case REQUIRE_DATA_BEGIN:
        dialog = new BaseYMDTimeDialogFragment();
        break;
      //结束时间
      case REQUIRE_DATA_END:
        dialog = new BaseYMDTimeDialogFragment();
        break;
      //状态
      case IFOVER:
        dialog = new ProjectSateDialogFragment();
        break;
      //经理
      //		case PM_MANAGER:
      //			dialog = new ProjectPmNameDialogFragment();
      //			break;
      //项目类别
      case KIND:
        dialog = ProjectPropertyDialogFrament.newInstance(ProjectPropertyDialogFrament.kind);
        break;
      //项目属性
      case TYPE:
        dialog = ProjectPropertyDialogFrament.newInstance(ProjectPropertyDialogFrament.type);
        break;

      default:
        break;
    }
    FragmentManager fm = getActivity().getSupportFragmentManager();
    dialog.setTargetFragment(SearchProjectCollectFragment.this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {

      return;
    }

    //客户单位
    if (requestCode == CUSTOMER) {
      customerName = data.getStringExtra("customerName");
      customerId = data.getStringExtra("customerId");
      mTvCustomerCompany.setText(customerName);
    }
    //性质
    //		if (requestCode == PROPERTY) {
    //			CustomerDialog customer = (CustomerDialog) data
    //					.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
    //			propertyName=customer.getName();
    //			propertyCode=customer.getId();
    //			mTvProjectProperty.setText(propertyName);
    //		}

    //阶段
    if (requestCode == STAGES) {
      CustomerDialog content =
          (CustomerDialog) data.getSerializableExtra(ProjectStagesDialogFragment.EXTRA_VALUE);
      projectStateName = content.getName();
      projectState = content.getId();
      mTvProjectStages.setText(projectStateName);
    }
    //开始日期
    if (requestCode == START_DATA_BEGIN) {
      Date mdate = (Date) data.getSerializableExtra(BaseTimeDialogFragment.EXTRA_DATE);
      startDateBegin = MyDateUtil.dateToString(mdate, MyDateUtil.TYPE_DATE);
      mTvStartDateBegin.setText(startDateBegin);
      return;
    }
    //结束日期
    if (requestCode == START_DATA_END) {
      Date startDate = (Date) data.getSerializableExtra(BaseTimeDialogFragment.EXTRA_DATE);
      startDateEnd = MyDateUtil.dateToString(startDate, MyDateUtil.TYPE_DATE);
      mTvStartDateEnd.setText(startDateEnd);
      return;
    }
    //项目名称
    //		if (requestCode==PROJECT_NAME) {
    //
    //		}

    //		//要求开始
    //		if (requestCode==REQUIRE_DATA_BEGIN) {
    //			Date startDate = (Date) data.getSerializableExtra(BaseTimeDialogFragment.EXTRA_DATE);
    //			requireDateBegin= MyDateUtil.dateToString(startDate, MyDateUtil.TYPE_DATE);
    //			mTvRequireDateBegin.setText(requireDateBegin);
    //		}
    //		//要求结束
    //		if (requestCode==REQUIRE_DATA_END) {
    //			Date startDate = (Date) data.getSerializableExtra(BaseTimeDialogFragment.EXTRA_DATE);
    //			requireDateEnd= MyDateUtil.dateToString(startDate, MyDateUtil.TYPE_DATE);
    //			mTvRequireDateEnd.setText(requireDateEnd);
    //		}
    //
    //		//阶段
    //		if (requestCode == IFOVER) {
    //			CustomerDialog content=(CustomerDialog) data.getSerializableExtra(ProjectSateDialogFragment.EXTRA_VALUE);
    //			ifOverName=content.getName();
    //			ifOver=content.getId();
    //			mTvifOver.setText(ifOverName);
    //		}
    //项目经理
    if (requestCode == PM_MANAGER) {
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      pmName = customer.getName();
      pmId = customer.getId();
      mTvPmManager.setText(pmName);
    }
    //类别
    if (requestCode == KIND) {
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      projectKindName = customer.getName();
      projectKindCode = customer.getId();
      mTvProjectKindCode.setText(projectKindName);
    }
    //		//属性
    //		if (requestCode==TYPE) {
    //			CustomerDialog customer = (CustomerDialog) data
    //					.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
    //			projectTypeName=customer.getName();
    //			projectTypeCode=customer.getId();
    //			mTvProjectTypeCode.setText(projectTypeName);
    //		}
    //		//业务员
    //		if (requestCode==SALE_NAME) {
    //			CustomerDialog customer = (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
    //			salesName=customer.getName();
    //		    salesId=customer.getId();
    //			mTvSalesName.setText(salesName);
    //		}

    super.onActivityResult(requestCode, resultCode, data);
  }

  public static SearchProjectCollectFragment newInstence(ProjectSearchContent content) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_CONTENT, content);
    SearchProjectCollectFragment mfragment = new SearchProjectCollectFragment();
    mfragment.setArguments(bundle);
    return mfragment;
  }
}

